$(function() {
	
	var app = angular.module('myApp', []);
	app.controller('myCtrl', function($scope, $http) {
		$scope.registerClick = function() {
			if ($("#username").val().trim() == null
					|| $("#username").val().trim() == "") {
				$scope.errorTip = '请输入用户名';
			} else if ($("#password").val().trim() == null
					|| $("#password").val().trim() == "") {
				$scope.errorTip = '请输入密码';
			} else if ($("#name").val().trim() == null
					|| $("#name").val().trim() == "") {
				$scope.errorTip = '请输入姓名';
			} else {
				$http({
					method : 'post',
					url : 'user/registerUser.do',
					headers : {
						'Content-Type' : 'application/x-www-form-urlencoded'
					},
					params : {
						login : encodeURI($("#username").val().trim()),
						pass : encodeURI($("#password").val().trim()),
						name : encodeURI($("#name").val().trim())
					}, // 传递数据作为字符串，从前台传到后台
				}).success(
						function(data) {// 这里的data，就是后台传递过来的数据
							if (data.msg == "error") {
								$scope.errorTip = '用户添加失败';
							} else {
								var location = (window.location + '')
										.split('/');
								var basePath = location[0] + '//' + location[2]
										+ '/' + location[3];
								window.location = basePath
										+ "/user/userMain.do";
							}
						}).error(function(data, status, headers, config) {
					$scope.errorTip = '用户添加失败';
				});
			}
		};
	});
	if (navigator.userAgent.toLowerCase().indexOf("chrome") != -1) {
		var selectors = document.getElementsByTagName("input");
		for (var i = 0; i < selectors.length; i++) {
			if ((selectors[i].type !== "button")
					&& (selectors[i].type !== "password")) {
				selectors[i].value = " ";
			}
		}
		setTimeout(function() {
			for (var i = 0; i < selectors.length; i++) {
				if (selectors[i].type !== "button") {
					selectors[i].value = "";
				}
			}
		}, 100)
	}
});
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};
